package com.iwinding.ladygg.widget.bottomBar;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.iwinding.ladygg.R;

/**
 * Created by Administrator on 2016/6/20.
 */
public class BottomBar extends LinearLayout implements View.OnClickListener {

    private MenuItem[] menuItems;
    private OnMenuTabClickListener tabClickListener;

    private int selectIndex = 0;

    public BottomBar(Context context) {
        super(context);
    }

    public BottomBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BottomBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setSelectIndex(int selectIndex) {
        this.selectIndex = selectIndex;
    }

    public int getSelectIndex() {
        return selectIndex;
    }

    public void setOnMenuTabClickListener(OnMenuTabClickListener tabClickListener) {
        this.tabClickListener = tabClickListener;
    }

    public void setMenuItems(Activity activity, int res_menulay) {
        clearMenuItems();
        PopupMenu popupMenu = new PopupMenu(activity, null);
        Menu menu = popupMenu.getMenu();
        activity.getMenuInflater().inflate(res_menulay, menu);
        int menuSize = menu.size();
        menuItems = new MenuItem[menuSize];

        for (int i = 0; i < menuSize; i++) {
            menuItems[i] = menu.getItem(i);
        }
        updateMenuItems(menuItems);
    }

    public void updateMenuItems(MenuItem[] items) {
        if (items != null) {
            int tabId = 0;
            for (MenuItem menuItem : items) {
                View tabView = View.inflate(getContext(), R.layout.base_bottom_bar, null);
                LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.weight = 1;
                tabView.setLayoutParams(params);
                ImageView icon = (ImageView) tabView.findViewById(R.id.bot_tab_icon);
                TextView tv = (TextView) tabView.findViewById(R.id.bot_tab_tv);
                icon.setImageDrawable(menuItem.getIcon());
                tv.setText(menuItem.getTitle());
                tabView.setOnClickListener(this);
                tabView.setTag(tabId);
                if (selectIndex==tabId) {
                    tabView.setSelected(true);
                    if (tabClickListener != null) {
                        tabClickListener.onMenuTabClick(tabView, selectIndex,menuItems[selectIndex].getTitle());
                    }
                }
                this.addView(tabView);
                tabId++;
            }
        }

    }

    @Override
    public void onClick(View v) {
        int oldindex = selectIndex;
        selectIndex = (Integer) v.getTag();
        if (oldindex == selectIndex) {
            return;
        }
        View oldTabView = getChildAt(oldindex);
        View newTabView = getChildAt(selectIndex);
        oldTabView.setSelected(false);
        newTabView.setSelected(true);

        if (tabClickListener != null) {
            tabClickListener.onMenuTabClick(v, selectIndex,menuItems[selectIndex].getTitle());
        }
    }
    
    public interface OnMenuTabClickListener {
        void onMenuTabClick(View view, int postion, CharSequence title);
    }

    public void clearMenuItems() {
        int childCount = this.getChildCount();
        if (childCount > 0) {
            this.removeAllViews();
        }

        if (menuItems != null && menuItems.length > 0) {
            menuItems = null;
        }
    }


}
